﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System.ComponentModel.Composition;
using Caliburn.Micro;
using OI.Events;

namespace OI.ViewModels
{
    [Export(typeof(TopRightViewModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class TopRightViewModel : Conductor<IScreen>, 
        IHandle<EnablePhonebook>
    {
        readonly IEventAggregator Events;

        // whether we show it all
        public bool ShowPhonebook
        {
            get { return _ShowPhonebook; }
            set 
            { 
                _ShowPhonebook = value;
                NotifyOfPropertyChange(() => ShowPhonebook);
            }
        }
        private bool _ShowPhonebook;

        // bound
        public double PhonebookOpacity
        {
            get
            {
                return PhonebookEnabled ? 0.8 : 0.2;
            }
        }

        // whether it is in use (enabled) or not
        public bool PhonebookEnabled
        {
            get { return _PhonebookEnabled; }
            set 
            { 
                _PhonebookEnabled = value;
                NotifyOfPropertyChange(() => PhonebookOpacity);
            }
        }
        private bool _PhonebookEnabled;


        [ImportingConstructor]
        public TopRightViewModel(IEventAggregator events)
        {
            Events = events;
            Events.Subscribe(this);
        }


        public void LoadSearchPanel(bool showPhonebook)
        {
            ActivateItem(IoC.Get<SearchPanelViewModel>());
            ShowPhonebook = showPhonebook;

            // setup the searchbox
            Events.Publish(new ConfigureSearchBox
            {
                IsEnabled = true,
                Watermark = "SEARCH",
                InitialText = ""
            });

            // if we are showing the phonebook, go ahead and enable it
            if (ShowPhonebook)
            {
                Events.Publish(new EnablePhonebook
                {
                    Value = true
                });
            }
        }


        public void LoadDatasetDetailsOptionsPanel()
        {
            var dop = IoC.Get<DatasetDetailsOptionsPanelViewModel>();
            ActivateItem(dop);
            ShowPhonebook = false;
        }


        // bound by convention
        // clicking the phonebook icon turns on this feature (see below)
        public void PhonebookClick()
        {
            Events.Publish(new EnablePhonebook
            {
                Value = true
            });
        }


        // gestures are routed indirectly through event aggregator so we can automate UI
        public void Handle(EnablePhonebook message)
        {
            PhonebookEnabled = message.Value;
        }


        public void Close()
        {
            this.CloseItem(ActiveItem);
            ShowPhonebook = false;
        }
    }
}
